package com.foreverwin.mes.production.support;

import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.core.utils.DateUtil;
import com.foreverwin.mes.core.utils.SpringUtil;
import com.foreverwin.mes.meapi.service.InventoryService;
import com.foreverwin.mes.production.dto.ResourceInventoryDto;
import com.foreverwin.mes.production.model.ResourceInventory;
import com.foreverwin.mes.production.model.ResourceInventoryLog;
import com.foreverwin.mes.production.service.ResourceInventoryLogService;
import com.foreverwin.mes.production.service.ResourceInventoryService;

import java.util.Date;

/**
 * @author Ervin Chen
 * @date 2020/2/5 12:03
 */
public class ItemBatchNumberUnloadStrategy implements UnloadStrategy {

    private InventoryService inventoryService;
    private ResourceInventoryService resourceInventoryService;
    private ResourceInventoryLogService resourceInventoryLogService;

    @Override
    public void initService() {
        inventoryService = SpringUtil.getBean(InventoryService.class);
        resourceInventoryService = SpringUtil.getBean(ResourceInventoryService.class);
        resourceInventoryLogService = SpringUtil.getBean(ResourceInventoryLogService.class);
    }

    @Override
    public boolean validate(ResourceInventoryDto resourceInventoryDto) {
        return true;
    }

    @Override
    public boolean unload(ResourceInventoryDto resourceInventoryDto) {
        String resource = resourceInventoryDto.getResrce();
        String itemBo = resourceInventoryDto.getItemBo();
        String inventoryId = resourceInventoryDto.getInventoryId();
        String vendorLot = resourceInventoryDto.getVendorLot();
        Double qty = resourceInventoryDto.getQty();
        String loadMode = resourceInventoryDto.getLoadMode();
        String site = CommonMethods.getSite();
        String userBo = CommonMethods.getUserBo();
        Date now = new Date();

        //删除设备库存
        String resourceInventoryHandle = HandleEnum.RESOURCE_INVENTORY.getHandle( site, resource, itemBo, vendorLot, "" );
        resourceInventoryService.deleteById( resourceInventoryHandle );

        //保存上料记录
        String resourceInventoryLogHandle = HandleEnum.RESOURCE_INVENTORY_LOG.getHandle( site, resource, inventoryId, DateUtil.format( now ) );
        ResourceInventoryLog resourceInventoryLog = new ResourceInventoryLog();
        resourceInventoryLog.setHandle(resourceInventoryLogHandle);
        resourceInventoryLog.setSite(site);
        resourceInventoryLog.setResrce(resource);
        resourceInventoryLog.setItemBo(itemBo);
        resourceInventoryLog.setInventoryId(inventoryId);
        resourceInventoryLog.setVendorLot(vendorLot);
        resourceInventoryLog.setLoadMode(loadMode);
        resourceInventoryLog.setCreatedDateTime(now);
        resourceInventoryLog.setCreateUserBo(userBo);
        resourceInventoryLogService.insert(resourceInventoryLog);

        return true;
    }
}
